package com.boot.project.service.impl;

import com.boot.project.dao.CourtDao;
import com.boot.project.dao.CourtTimeDao;
import com.boot.project.dao.OrderDao;
import com.boot.project.dao.UserInfoDao;
import com.boot.project.domain.CourtDO;
import com.boot.project.domain.CourtTimeDO;
import com.boot.project.domain.OrderDo;
import com.boot.project.domain.UserInfoDo;
import com.boot.project.service.CourtService;
import com.boot.project.service.OrderService;
import com.fasterxml.jackson.databind.node.IntNode;
import javafx.beans.binding.ObjectExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private CourtDao courtDao;
    @Autowired
    private CourtTimeDao courtTimeDao;

    @Override
    public int add(OrderDo orderDo) {
        return orderDao.add(orderDo);
    }

    @Override
    public Map<String, Object> list(Integer pageNum, Integer pageSize) {
        Integer startIndex = (pageNum - 1) * pageSize;
        Map<String, Object> params = new HashMap<>();
        params.put("startIndex", startIndex);
        params.put("pageSize", pageSize);
        Integer count = orderDao.count();
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("total", count);
        resultMap.put("data", orderDao.list(params));
        return resultMap ;
    }

    @Override
    public Map<String, Object> listBy(String openId, Integer pageNum, Integer pageSize) {
        Map<String, Object> params = new HashMap<>();
        Integer startIndex = (pageNum -1) * pageSize;
        params.put("openId", openId);
        params.put("startIndex", startIndex);
        params.put("pageSize", pageSize);
        Integer listByCount = orderDao.listByCount(openId);
        Map<String, Object> result = new HashMap<>();
        result.put("data",orderDao.listBy(params));
        result.put("listByCount",listByCount);
        return result;
    }

    @Override
    public List<OrderDo> listByopenId(OrderDo orderDo) {
        return orderDao.listByopenId(orderDo);
    }

    @Override
    public int remove(OrderDo orderDo) {
        return orderDao.remove(orderDo);
    }

    @Override
    public Map<String, Object> changeOrderStatus(String openId) {
        Map<String, Object> mapResult = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        params.put("openId", openId);
        params.put("payTime", new Date());
        Integer changeFlag = orderDao.changeOrderStatus(params);
        if (changeFlag >= 0) {
            mapResult.put("msg", "ok");
            mapResult.put("code", 200);
            return mapResult;
        } else {
            mapResult.put("msg", "error");
            mapResult.put("code", 500);
            return mapResult;
        }
    }

    @Override
    public int update(OrderDo orderDo) {
        return orderDao.update(orderDo);
    }

    @Override
    public List<OrderDo>
    unpaid(String openId) {
        return orderDao.unpaid(openId);
    }

    @Override
    public List<OrderDo> paid(String openId) {
        return orderDao.paid(openId);
    }

    @Override
    public List<Map<String, Object>> orderRecord(String openId) {
        List<Map<String, Object>> result = new ArrayList<>();
        List<OrderDo> orderDos = orderDao.orderRecord(openId);
        String userName = userInfoDao.getUserName(openId);
        for (OrderDo i: orderDos) {
            Map<String, Object> temp = new HashMap<>();
            temp.put("orderNO", i.getOrderId());
            temp.put("orderType", i.getCommodityId());
            temp.put("orderTime", i.getPayTime());
            temp.put("orderPrice", i.getCash());
            temp.put("userName", userName);
            CourtDO courtDO = courtDao.getByCourtId(i.getCourtId());
            temp.put("courtDate", courtDO.getDateTime());
            temp.put("courtName", courtDO.getCourtName());
            CourtTimeDO courtTimeDO = courtTimeDao.getByCourtTimeId(i.getCourtTimeId());
            double s = courtTimeDO.getStartTime();
            DecimalFormat sDf = new DecimalFormat("#.00");
            double e = courtTimeDO.getEndTime();
            DecimalFormat eDf = new DecimalFormat("#.00");
            temp.put("courtStartTime", sDf.format(s));
            temp.put("courtEndTime", eDf.format(e));
            result.add(temp);
        }
        return result;
    }
}
